package com.fw.ui.view.settings.usercontrol.parentmerchant.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.bean.SearchCriteriaBean;
import com.fw.service.bean.SearchResultBean;
import com.fw.service.merchantprofile.bean.MerchantProfileBean;
import com.fw.service.merchantprofile.bean.SearchMerchantProfileBean;
import com.fw.ui.bao.MerchantProfileBAO;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.bean.parentmerchant.SearchMerchantProfileTableItemBean;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.common.PagedTable;
import com.fw.ui.view.common.TablePanel;
import com.fw.ui.view.event.AbstractButtonClickListener;
import com.fw.ui.view.settings.usercontrol.parentmerchant.EditParentMerchantView;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button.ClickEvent;

@Component
@Scope(SCOPE_PROTOTYPE)
public class EditParentMerchantSelectedMerchantProfileTableRemoveButtonClickListener extends AbstractButtonClickListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;
    @Autowired
    private PropertyBAO propertyBAO;
    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private MerchantProfileBAO merchantProfileBAO;

    @SuppressWarnings("rawtypes")
    @Override
    public Map<String, Object> click(ClickEvent event) throws Exception {
	EditParentMerchantView editParentMerchantView = (EditParentMerchantView) this.getBasedComponent();
	SearchMerchantProfileTableItemBean searchMerchantProfileTableItemBean = (SearchMerchantProfileTableItemBean) event.getButton()
		.getData();
	SearchCriteriaBean<SearchMerchantProfileBean> searchCriteriaBean = editParentMerchantView.getSearchCriteriaBean();
	SearchMerchantProfileBean criteriaBean = searchCriteriaBean.getCriteriaBean();
	List<Long> excludeMerchantIds = criteriaBean.getExcludeMerchantIds();
	if (excludeMerchantIds == null) {
	    excludeMerchantIds = new ArrayList<Long>();
	}
	Long selectedMerchantId = searchMerchantProfileTableItemBean.getId();
	if (selectedMerchantId != null) {
	    // search new
	    excludeMerchantIds.remove(selectedMerchantId);
	    searchCriteriaBean.setCriteriaBean(criteriaBean);
	    SearchResultBean<MerchantProfileBean> searchResultBean = merchantProfileBAO.selectMerchantProfile(searchCriteriaBean);
	    Integer totalPage = searchResultBean.getTotalPages();
	    if (totalPage == null) {
		totalPage = 1;
	    }
	    Long totalElement = searchResultBean.getTotalElements();
	    if (totalElement == null) {
		totalElement = 0l;
	    }
	    List<BeanItem> beanItems = editParentMerchantView.generateTableItemBean(searchResultBean);
	    editParentMerchantView.getTableMerchantPanel().clearTableData();
	    editParentMerchantView.getTableMerchantPanel().setTableBeanContainerDataTotalPage(beanItems, totalPage, totalElement);

	    // remove item
	    TablePanel tableMerchantSelectedPanel = editParentMerchantView.getTableMerchantSelectedPanel();
	    PagedTable pagedSelectedTable = tableMerchantSelectedPanel.getTable();
	    Map<Long, BeanItem> selectedBeanItemMap = editParentMerchantView.getSelectedBeanItemMap();
	    selectedBeanItemMap.remove(selectedMerchantId);
	    List<BeanItem> selectedBeanItems = new ArrayList<BeanItem>(selectedBeanItemMap.values());
	    int totalElements = selectedBeanItems.size();
	    Integer itemPerPage = Integer.parseInt((String) pagedSelectedTable.getManualItemsPerPageSelect().getValue());
	    totalPage = Double.valueOf(Math.ceil(Double.valueOf(totalElements) / Double.valueOf(itemPerPage))).intValue();
	    List<BeanItem> subItems = new ArrayList<BeanItem>();
	    if (totalElements > 1) {
		subItems = selectedBeanItems.subList((totalPage - 1) * itemPerPage, totalElements);
	    } else {
		subItems = selectedBeanItems;
	    }
	    tableMerchantSelectedPanel.clearTableData();
	    tableMerchantSelectedPanel.setManualPageTable(totalPage);
	    tableMerchantSelectedPanel.setTableBeanContainerDataTotalPage(subItems, totalPage, Long.valueOf(totalElements));
	}
	return null;
    }

    @Override
    public boolean validate(ClickEvent event) {
	return true;
    }

}
